from django.db import models
from django.utils import timezone
from company.models import UserProfile


# 行业趋势表
class IndustryTrend(models.Model):
    industry_name = models.CharField(max_length=100, verbose_name='行业名称', db_comment='行业名称')
    trend_title = models.CharField(max_length=200, verbose_name='趋势标题', db_comment='趋势标题')
    content = models.TextField(verbose_name='趋势内容', db_comment='趋势内容')
    source_url = models.URLField(blank=True, null=True, verbose_name='数据来源', db_comment='数据来源URL')
    trend_score = models.FloatField(default=0.0, verbose_name='趋势指数', db_comment='趋势指数')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_industry_trends'
        verbose_name = '行业趋势'
        verbose_name_plural = '行业趋势'
        ordering = ['-created_at']
        db_table_comment = '行业趋势表'

    def __str__(self):
        return f"{self.industry_name} - {self.trend_title}"


# 竞品分析表
class CompetitorAnalysis(models.Model):
    competitor_name = models.CharField(max_length=200, verbose_name='竞品名称', db_comment='竞品名称')
    industry = models.CharField(max_length=100, verbose_name='所属行业', db_comment='所属行业')
    pricing = models.TextField(verbose_name='定价策略', db_comment='定价策略')
    positioning = models.TextField(verbose_name='市场定位', db_comment='市场定位')
    strengths = models.TextField(verbose_name='优势', db_comment='优势')
    weaknesses = models.TextField(verbose_name='劣势', db_comment='劣势')
    market_share = models.FloatField(default=0.0, verbose_name='市场份额(%)', db_comment='市场份额百分比')
    created_by = models.ForeignKey(
        UserProfile,
        on_delete=models.SET_NULL,
        null=True,
        related_name='competitor_analyses',
        verbose_name='创建者',
        db_comment='创建者ID'
    )
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        db_table = 'mentorx_competitor_analyses'
        verbose_name = '竞品分析'
        verbose_name_plural = '竞品分析'
        ordering = ['-created_at']
        db_table_comment = '竞品分析表'

    def __str__(self):
        return self.competitor_name


# 市场报告表
class MarketReport(models.Model):
    title = models.CharField(max_length=200, verbose_name='报告标题', db_comment='报告标题')
    industry = models.CharField(max_length=100, verbose_name='所属行业', db_comment='所属行业')
    summary = models.TextField(verbose_name='报告摘要', db_comment='报告摘要')
    key_findings = models.TextField(verbose_name='关键发现', db_comment='关键发现')
    opportunities = models.TextField(verbose_name='市场机会', db_comment='市场机会')
    threats = models.TextField(verbose_name='市场威胁', db_comment='市场威胁')
    report_data = models.JSONField(default=dict, verbose_name='报告数据', db_comment='报告数据JSON')
    generated_by = models.ForeignKey(
        UserProfile,
        on_delete=models.SET_NULL,
        null=True,
        related_name='market_reports',
        verbose_name='生成者',
        db_comment='生成者ID'
    )
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_market_reports'
        verbose_name = '市场报告'
        verbose_name_plural = '市场报告'
        ordering = ['-created_at']
        db_table_comment = '市场报告表'

    def __str__(self):
        return self.title
